﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using VolunteerManager.Model;

namespace VolunteerManager.ViewModel
{
    public class ProgramViewModel : ViewModelBase
    {

        private ObservableCollection<ProgramLog> _volutterList;

        IDataService _dataService ;

        private ICommand _editCommand;
        private ICommand _delCommand;
        private ICommand _createCommand;
        private ICommand _refurbishCommand;
        private ICommand _searchCommand;
        private ICommand _typeSelectCommand;
        private ICommand _exportCommand;
        private int _total;
        private string _word;

        public ProgramViewModel(IDataService dataService)
        {
            _dataService = dataService;
            refurbish();
        }

        public ICommand SelectedTypeCommand
        {
            get
            {
                if (null == _typeSelectCommand)
                {
                    _typeSelectCommand = new RelayCommand<object>((a) => selectatype(a));
                }
                return _typeSelectCommand;
            }
        }

        public ICommand CreateCommand
        {
            get
            {
                if (null == _createCommand)
                {
                    _createCommand = new RelayCommand<object>((a) => create(a));
                }
                return _createCommand;
            }
        }

        private void create(object o){
            Windows.CreateProgram cp = new Windows.CreateProgram();
            cp.ShowDialog();
        }

        /// <summary>
        /// 刷新界面内容
        /// </summary>
        private void refurbish()
        {
            ProgramLog v = new ProgramLog();
            this.VolutterList = new ObservableCollection<ProgramLog>(v.FindAll2<ProgramLog>("1=1"));
            
            Totla = v.Count("1=1");
        }


        public ObservableCollection<ProgramLog> VolutterList
        {
            get
            {
                return _volutterList;
            }
            set
            {
                _volutterList = value;
                RaisePropertyChanged("VolutterList");
            }
        }

        private void selectatype(object a)
        {
            if (a is ProgramType)
            {
                ProgramType b = a as ProgramType;
                int w = b.ID;

                this.VolutterList = new ObservableCollection<ProgramLog>(new ProgramLog().FindAll2<ProgramLog>("ProgramType=" + w));
            }
            else if (a is System.Windows.Controls.TreeViewItem)
            {
                System.Windows.Controls.TreeViewItem b = a as System.Windows.Controls.TreeViewItem;
                if (b.Tag != null)
                {
                    if (b.Tag.ToString() == "-1")
                    {
                        refurbish();
                    }
                }
                else
                {
                    //string w = b.Header.ToString();

                    //this.VolutterList = new ObservableCollection<Voluteer>(new Voluteer().FindAll2<Voluteer>("Models LIKE '%" + w + "%'"));
                    //Word = "";
                }
            }
        }

        #region 界面绑定属性
        public int Totla
        {
            get { return _total; }
            set
            {
                _total = value;
                RaisePropertyChanged("Totla");
            }
        }
        #endregion
    }
}
